<?php
namespace Install\Controller;
use Think\Controller;
use Think\Db;
/**
 * 首页
 */
class IndexController extends Controller {
	
	function _initialize(){
		if(file_exists_case(MODULE_PATH."install.lock")){
			redirect(__ROOT__."/");
		}
	}
	
    public function index(){
    	$this->display();
    }
    
    
    public function step2(){
    	$data=array();
    	$data['phpversion'] = @ phpversion();
    	$data['os']=PHP_OS;
    	$tmp = function_exists('gd_info') ? gd_info() : array();
    	$server = $_SERVER["SERVER_SOFTWARE"];
    	$host = (empty($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_HOST"] : $_SERVER["SERVER_ADDR"]);
    	$name = $_SERVER["SERVER_NAME"];
    	$max_execution_time = ini_get('max_execution_time');
    	$allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
    	$allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
    	$safe_mode = (ini_get('safe_mode') ? '<font color=red>[×]On</font>' : '<font color=green>[√]Off</font>');
    	
    	$err = 0;
    	if (empty($tmp['GD Version'])) {
    		$gd = '<font color=red>[×]Off</font>';
    		$err++;
    	} else {
    		$gd = '<font color=green>[√]On</font> ' . $tmp['GD Version'];
    	}
    	
    	if (class_exists('pdo')) {
    		$data['pdo'] = '<i class="fa fa-check correct"></i> 已开启';
    	} else {
    		$data['pdo'] = '<i class="fa fa-remove error"></i> 未开启';
    		$err++;
    	}
    	
    	if (extension_loaded('pdo_mysql')) {
    		$data['pdo_mysql'] = '<i class="fa fa-check correct"></i> 已开启';
    	} else {
    		$data['pdo_mysql'] = '<i class="fa fa-remove error"></i> 未开启';
    		$err++;
    	}
    	
    	if (ini_get('file_uploads')) {
    		$data['upload_size'] = '<i class="fa fa-check correct"></i> ' . ini_get('upload_max_filesize');
    	} else {
    		$data['upload_size'] = '<i class="fa fa-remove error"></i> 禁止上传';
    	}
    	
    	if (function_exists('session_start')) {
    		$data['session'] = '<i class="fa fa-check correct"></i> 支持';
    	} else {
    		$data['session'] = '<i class="fa fa-remove error"></i> 不支持';
    		$err++;
    	}
    	
    	$folders = array(
    			'',//根目录
    			'Runtime',
    			'Runtime/Cache',
    			'Runtime/Data',
    			'Runtime/Logs',
    			'Runtime/Temp',
    	);
    	$new_folders=array();
    	foreach($folders as $dir){
    		$Testdir = "./".$dir;
    		sp_dir_create($Testdir);
    		if(sp_testwrite($Testdir)){
    			$new_folders[$dir]['w']=true;
    		}else{
    			$new_folders[$dir]['w']=false;
    			$err++;
    		}
    		if(is_readable($Testdir)){
    			$new_folders[$dir]['r']=true;
    		}else{
    			$new_folders[$dir]['r']=false;
    			$err++;
    		}
    	}
    	$data['folders']=$new_folders;
    	
    	$this->assign($data);
    	$this->display();
    }

    public function step3(){
    	$modules = get_dir_file_list(substr(APP_PATH,0,strlen(APP_PATH)-1),array(''));  //分组列表
    	$installModules = array();
    	foreach($modules as $md){
    		//显示能够安装的分组
    		$tmpDir = APP_PATH.$md."/Data";
    		if(!file_exists_case($tmpDir."/install.lock")){
    			$tmpFiles = get_dir_file_list($tmpDir);
    			//如果存在同分组名的.sql文件，表示该分组可安装
    			if(in_array($md.'.sql', $tmpFiles)){
    				$installModules[] = $md;
    			}
    		}
    	}
    	$this->installModules = $installModules;
    	$this->display();
    }
    
    public function step4(){
    	if(IS_POST){
    		//创建数据库 ,读取Common配置文件
    		$dbconfig['DB_TYPE']= C('DB_TYPE');
    		$dbconfig['DB_HOST']= C('DB_HOST');
    		$dbconfig['DB_USER']= C('DB_USER');
    		$dbconfig['DB_PWD'] = C('DB_PWD');
    		$dbconfig['DB_PORT']= C('DB_PORT');
    		$db  = Db::getInstance($dbconfig);
    		$dbname=strtolower(C('DB_NAME'));
    		$sql = "CREATE DATABASE IF NOT EXISTS `{$dbname}` DEFAULT CHARACTER SET utf8";
    		$db->execute($sql) || $this->error($db->getError());
    		
    		$this->display();
    		
    		//创建数据表
    		$dbconfig['DB_NAME']=$dbname;
    		$dbconfig['DB_PREFIX']=trim(C('DB_PREFIX'));
    		$db  = Db::getInstance($dbconfig);
    		
    		$table_prefix = C('DB_PREFIX');
    		
    		foreach($_POST['installMDs'] as $md){
    			sp_execute_sql($db, APP_PATH.$md.'/Data/'.$md.'.sql', $table_prefix);
    			@touch( APP_PATH.$md.'/Data/install.lock');  //生成lock文件
    		}
    		
    		session("_install_step",4);
    		sleep(1);
    		$this->redirect("step5");
    		

    	}else{
            session("_install_step",4);
            sleep(1);
            $this->redirect("step5");
        }
    }
    
    public function step5(){
    	if(session("_install_step")==4){
    		session("_install_step",null);
    		$this->display();
    	}else{
    		$this->error("非法安装！");
    	}
    	 
    }
}

